/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.rpc.service.impl  
 */
package com.apache.rpc.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.rpc.manager.DynamicDatabaseManager;

/**
 * description:  
 * @author Hou Dayu 创建时间：2016-1-19
 * @Copyright(c)2014:
 */
public class DynamicDatabaseManagerImpl implements DynamicDatabaseManager {

	private IDao dynamicDatabaseDao;

	/**
	 * 复制数据到另一张表中
	 * TODO 需要保持两个表的结构完全相同 
	 */
	public String saveInfo(ParamsVo vo) throws BusinessException {
		//DynamicDatabase
		String key = Validator.getDefaultStr(vo.getKey(), "DynamicDatabase");
		String newTableName = String.valueOf(vo.getParams("targetTableName"));
		String oldTableName = String.valueOf(vo.getParams("sourceTableName"));
		String condition = String.valueOf(vo.getParams("condition"));
		String flag = String.valueOf(vo.getParams("isDelete"));
		String columns = Validator.getDefaultStr(String.valueOf(vo.getParams("columns")), "");
		MethodParam param = new MethodParam(key, "", "", "com.apache.rpc.entity.DynamicDatabase");
		String sql = getDyncSql(newTableName, oldTableName, condition, columns);
		param.setParams("dyncSql", sql);
		dynamicDatabaseDao.select(param);//.insert(param);
		if ("true".equalsIgnoreCase(flag)) {
			sql = "DELETE FROM " + oldTableName + " where 1=1 " + Validator.getDefaultStr(condition, "");
			param.setParams("dyncSql", sql);
			param.setDelete(true);
			dynamicDatabaseDao.delete(param);
		}
		return "true";
	}

	private String getDyncSql(String newTableName, String oldTableName, String condition, String columns) {
		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO " + newTableName);
		sb.append(" select ");
		if (Validator.isNull(columns)) {
			sb.append(" * from ");
		} else {
			sb.append(columns + " from ");
		}
		sb.append(oldTableName);
		if (Validator.isNotNull(condition))
			sb.append(" where 1=1 " + condition);
		return sb.toString();
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 */
	public boolean editInfo(ParamsVo vo) throws BusinessException {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 */
	public boolean deleteInfo(ParamsVo vo) throws BusinessException {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 */
	public Object execute(ParamsVo vo) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 */
	public Object getInfoById(ParamsVo vo) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 */
	public List getList(ParamsVo vo) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 */
	public long countInfo(ParamsVo vo) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setDynamicDatabaseDao(IDao dynamicDatabaseDao) {
		this.dynamicDatabaseDao = dynamicDatabaseDao;
	}

	@Override
	public Page getPageInfo(ParamsVo vo) {
		return null;
	}
}
